package com.example.eguser1.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.example.eguser1.mapper.UserMapper;
import com.example.eguser1.pojo.dto.UserPrarm;
import com.example.eguser1.pojo.entity.User;
import com.example.eguser1.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    //1.添加用户
    @PostMapping("addUser")
    public String addUser(UserPrarm userPrarm) {
        User user = new User();
        BeanUtils.copyProperties(userPrarm, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int i = userMapper.addUser(user);
        if (i > 0) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }
    //2.查询用户
    @GetMapping("getUser")
    public List<UserListVO> getuser(){
        List<UserListVO> userListVO=userMapper.getUser();
        return userListVO;
    }
    //3.删除用户
    @PostMapping("deleteUser")
    public String deleteUser(Long id){
        int i = userMapper.deleteUser(id);
        if (i>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
}